package com.quyunshuo.module.home.dialog

import android.os.Bundle
import android.view.Gravity
import com.quyunshuo.module.home.databinding.DialogTestBinding
import com.quyunshuo.module.home.util.dp

class TestDialog : BaseDialog<DialogTestBinding>() {
    private var title:String? = null
    private var cancel:String? = null
    private var comfirm:String? = null
    companion object {
        /*
            当需要修改创建逻辑时，只需改动一处代码
            避免散落在各处的直接new TestDialog()调用
         */
        fun newInstance(): TestDialog {
            return TestDialog()
        }
    }
    fun setTitleText(text:String){
        title = text
    }
    fun setCancelText(text:String){
        cancel = text
    }
    fun setComfirmText(text:String){
        comfirm = text
    }

    private var callback: (() -> Unit?)? = null
    fun setCallback(callback: () -> Unit) {
        this.callback = callback
    }

    override fun isCancelable(): Boolean {
        return super.isCancelable()
    }

    override fun getGravity(): Int {
        return Gravity.BOTTOM
    }

    override fun createViewBinding(): DialogTestBinding {
        return DialogTestBinding.inflate(layoutInflater)
    }

    override fun init(savedInstanceState: Bundle?) {
        title?.let { binding.title.text =it }
        cancel?.let { binding.cancel.text =it }
        comfirm?.let { binding.comfirm.text =it }
        binding.cancel.setOnClickListener {
            dismissAllowingStateLoss()
        }
        binding.comfirm.setOnClickListener {
            callback?.invoke()
            dismissAllowingStateLoss()

        }
    }


    override fun getWidth(): Int {

        if (isTabletDevice(resources.configuration)) {
            return 600.dp
        } else {
            return 280.dp

        }

    }

    override fun cancel(): Boolean {
        return true
    }

}